/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.web;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.mapper.JsonMapper;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.expert.entity.Product;
import com.chlitina.store.modules.expert.entity.ProductCategory;
import com.chlitina.store.modules.expert.service.ProductCategoryService;
import com.chlitina.store.modules.expert.service.ProductService;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * 产品Controller
 * @author lvbo
 * @version 2013-06-04
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/expert/product")
public class ProductController extends BaseController {
	@Autowired
	private ProductService productService;
	@Autowired
	private ProductCategoryService productCategoryService;
	
	@ModelAttribute
	public Product get(@RequestParam(required=false) Long id) {
		if (id != null){
			return productService.get(id);
		}else{
			return new Product();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Long category_id, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		ProductCategory parent = productCategoryService.get(category_id);
		Page<Product> page = new Page<Product>(request, response);
		Page<Product> current_page = productService.find(page, category_id); 
		model.addAttribute("parent", parent);
		model.addAttribute("page", current_page);
		return "modules/expert/productList";
	}
	
	@RequestMapping(value = {"detail", "desc"})
	public String desc(Product product, ModelMap modelMap) {
		modelMap.addAttribute("product", product);
		return "modules/expert/productDesc";
	}
	
	@RequestMapping(value = {"select"})
	public String select(@RequestParam(required=false) String name, ModelMap modelMap) {
		List<Product> productList = productService.findAllByName(name);
		modelMap.addAttribute("productList", productList);
		return "modules/expert/productSelect";
	}
	
	@ResponseBody
	@RequestMapping(value = "productData")
	public String treeDatas(@RequestParam(required=false) Long extId, HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		Set<Map<String, Object>> set = Sets.newHashSet();
		List<Product> productList = productService.findAllAvailable(Product.FLAG_ENABLE);
		for (Product product : productList) {
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", product.getId());
			map.put("name", product.getName());
			map.put("price", product.getRetail_price());
			set.add(map);
		}
		return JsonMapper.getInstance().toJson(set);
	}
}
